package top.tagao.study00demo.study02demo;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * JDBC工具类
 * @author Viktor.Liu
 * @Date 2023-12-14 14:23
 */
//JDBC工具类



@RestController
@PropertySource("classpath:application.yml")
public class JDBCDruidUtils {
    /*
    获取连接池对象
     */
    // @Getter
    // private static DataSource dataSource;

    @Getter
   // private static  DruidDataSource dataSource ;
    private static DataSource dataSource;

    @Value("${spring.datasource.druid.url}")
    private  String url;

    @Value("${spring.datasource.druid.username}")
    private  String username;

    @Value("${spring.datasource.druid.password}")
    private  String password;

    /*
   创建数据Properties集合对象加载加载配置文件
    */
    static {
        Properties pro = new Properties();
        //加载数据库连接池对象
        try {
            // 创建 Druid 连接池
            // DruidDataSource dataSource = new DruidDataSource();
            // dataSource.setUrl("jdbc:mysql://172.18.235.152:3306/test_db_iycbol_pc_lottery_server?characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=true&serverTimezone=Asia/Shanghai");
            // dataSource.setUsername("iycbol_pc_lottery_server");
            // dataSource.setPassword("TJ3KoE1OAYLX2ueg");

            //获取数据库连接池对象
            pro.load(JDBCDruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    获取连接
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }


    /**
     * 关闭conn,和 statement独对象资源
     *
     * @param connection
     * @param statement
     * @MethodName: close
     * @return: void
     */
    public static void close(Connection connection, Statement statement) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭 conn , statement 和resultset三个对象资源
     *
     * @param connection
     * @param statement
     * @param resultSet
     * @MethodName: close
     * @return: void
     */
    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        close(connection, statement);
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}